/*
 * @(#) MenuController.java 2018年1月24日 下午5:07:50
 *
 * Copyright 2018 CIMIP, Inc. All rights reserved.
 * H-VISIONS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package cn.hc.share.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hc.share.entity.Menu;
import cn.hc.share.entity.MenuTree;
import cn.hc.share.service.MenuService;
import cn.hc.share.util.JsonResult;
import cn.hc.share.util.StringUtil;

@Controller
@RequestMapping("/menu")
public class MenuController extends AbstractController 
{

	@Resource
	private MenuService menuService;

	// 更新目录
	@RequestMapping("/updateMenu.do")
	@ResponseBody
	public Object updateMenu(String id, String text, String parentId)
	{
		Menu menu=null;
		menu = menuService.findMenuByNodeId(id);
		if(menu!=null) {
			try
			{
				menu.setText(text);
				System.out.println("menu:"+menu.getText());
				int renameMenu = menuService.renameMenu(menu);
				Object obj = renameMenu;
				return new JsonResult(obj);
			} catch (Exception e)
			{
				return new JsonResult(e.getMessage());
			}
		}else {
			menu = new Menu(id, text,parentId);
			int num = 0;
			try
			{
				num = menuService.addMenu(menu);
				Object obj = num;
				return new JsonResult(obj);
			} catch (Exception e)
			{
				return new JsonResult(e.getMessage());
			}
		}
		

	}

	// 获取目录
		@RequestMapping("/deleteMenus.do")
		@ResponseBody
		public Object deleteMenus(String menuId)
		{
			
			List<String> menuIds = menuService.getMenuIds(menuId);
			menuIds.add(menuId);
			int num = menuService.deleteMenus(menuIds);
			Object obj=num;
			return new JsonResult(obj);
		}
	
	
	
	// 获取目录
	@RequestMapping("/showMenuTree.do")
	@ResponseBody
	public Object showMenuTree(HttpServletRequest req)
	{
		String id = req.getParameter("id");
		if (StringUtil.isNull(id))
		{
			id = "0";
		}
		
		List<MenuTree> list = menuService.showMenuTree(id);
		
		return new JsonResult(list);

	}

}
